import { defineStore } from 'pinia'
import { store } from '../index'
import { ElMessage } from 'element-plus'
import { CACHE_KEY, useCache } from '@/hooks/useCache'

const { wsCache } = useCache()

interface AppState {
  screenfull: boolean
  search: boolean
  size: boolean
  locale: boolean
  message: boolean
  title: string
  userInfo: string
  isDark: boolean
  
}

export const useAppStore = defineStore('app', {
  state: (): AppState => {
    return {
      userInfo: 'userInfo', // 登录信息存储字段-建议每个项目换一个字段，避免与其他项目冲突
      isDark: wsCache.get(CACHE_KEY.IS_DARK) || false, // 是否是暗黑模式
    }
    
  },
  getters: {
    getLocale(): boolean {
      return this.locale
    },
    
    getUserInfo(): string {
      return this.userInfo
    },
    getIsDark(): boolean {
      return this.isDark
    }
  },
  actions: {

    setIsDark(isDark: boolean) {
      this.isDark = isDark
      if (this.isDark) {
        document.documentElement.classList.add('dark')
        document.documentElement.classList.remove('light')
      } else {
        document.documentElement.classList.add('light')
        document.documentElement.classList.remove('dark')
      }
      wsCache.set(CACHE_KEY.IS_DARK, this.isDark)
    }
     
  },
  persist: false
})

export const useAppStoreWithOut = () => {
  return useAppStore(store)
}
